<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
    <link type="text/css" rel="stylesheet" href="${pageContext.request.contextPath}/layui/css/layui.css"/>
    <script type="text/javascript" src="${pageContext.request.contextPath}/layui/layui.js"></script>
</head>
<body>
<div style="width: 400px;height: 250px; margin: 0 auto;margin-top: 15%;">
<form class="layui-form" >
    <div class="layui-form-item">
        <label class="layui-form-label">账号:</label>
        <div class="layui-input-block">
            <input type="text" name="username" value="admin" lay-verify="required" autocomplete="off" placeholder="请输入账号" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">密码:</label>
        <div class="layui-input-block">
            <input type="password" name="password" value="123456" lay-verify="pass"  placeholder="请输入密码" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button type="button" class="layui-btn" lay-submit="" lay-filter="login">登录</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>
</div>
<script>
    layui.use(['form', 'jquery','layer'], function(){
        var form = layui.form
            ,layer = layui.layer
            ,$ = layui.jquery;
        //自定义验证规则
        form.verify({
            pass: [
                /^[\S]{6,12}$/
                ,'密码必须6到12位，且不能出现空格'
            ]

        });


        //监听提交
        form.on('submit(login)', function(data){
            $.ajax({
                url: "${pageContext.request.contextPath}/login/login",
                data: data.field,
                type: "post",
                success: function(result){
                    if(result.code===2000){
                        layer.msg(result.msg,{icon:1});
                        //重定向跳转 无法跳转到WEB-INF下。
                        //重定向到控制层----由控制层转发到WEB-INF下的页面
                        location.href="${pageContext.request.contextPath}/page/main";
                    }else{
                        layer.msg(result.msg,{icon:5});
                    }
                },
                dataType: "json"
            });
            return false;
        });
    });
</script>
</body>
</html>
